package com.foreveross.mofang.datapersistence.infra.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.foreveross.mofang.datapersistence.infra.service.IHabseMapToMap;

/**
 * 将json转换来的map数据转换成hbase持久化的map数据
 * 
 * @author fb
 */
public class JsonMapToHbaseMap implements IHabseMapToMap<String, Object>{
	
	private static final Logger logger = LoggerFactory.getLogger(JsonMapToHbaseMap.class);

	private String parentHierarchyPrefix = "";
	
	private Map<String, Object> jsonMap;
	
	private Map<String, Object> hbaseMap;
	
	
	public JsonMapToHbaseMap() {

	}
	
	public JsonMapToHbaseMap(Map<String, Object> jsonMap, Map<String, Object> hbaseMap) {
		super();
		this.jsonMap = jsonMap;
		this.hbaseMap = hbaseMap;
	}
	
	public JsonMapToHbaseMap(String parentHierarchyPrefix,
			Map<String, Object> jsonMap, Map<String, Object> hbaseMap) {
		super();
		this.parentHierarchyPrefix = parentHierarchyPrefix;
		this.jsonMap = jsonMap;
		this.hbaseMap = hbaseMap;
	}

	/**
	 * 数据转换 ,主要将map中的程序对象通过用-1、-1-1等key形式变成平级对象以方便hbase持久化
	 */
	public Map<String, Object> translation() {
		if (jsonMap == null || jsonMap.isEmpty()) {
			logger.debug("需要转换map为空，将反回一个空map对象");
			
			return hbaseMap;
		}
		
		for (Entry<String, Object> entry: jsonMap.entrySet()) {
			if (entry.getValue() instanceof List) {
				listMapOperation(entry.getKey(), entry.getValue());
				continue;
			}
			
			if (entry.getValue() instanceof Map) {
				mapOperation(entry.getKey(), entry.getValue());
				continue;
			}
			
			hbaseMap.put(getKey(entry.getKey()), entry.getValue());
		}
		
		
		return hbaseMap;
	}
	
	@SuppressWarnings("unchecked")
	private void listMapOperation(String key, Object obj) {
		List<Map<String, Object>> list = (List<Map<String, Object>>) obj;
		//Map<String, Object> childMap = new HashMap<String, Object>();
		String newPrefix = null;
		int currHierarchy = 0;
		
		logger.debug("集合{}，其值为：{}", key ,obj);
		
		// 如果不为空，且list中不为map对象
		if (!(list.isEmpty() || list.get(0) instanceof Map)) {
			hbaseMap.put(getKey(key), list.toString());
			return;
		}
		
		for (Map<String, Object> map: list) {
			// 当每便利次currHierarchy加1
			newPrefix = parentHierarchyPrefix + "_" + currHierarchy++;
			// 第一层如果是集合必需要new一个新map存入所有当前集合及其子集信息
			new JsonMapToHbaseMap(newPrefix, map, hbaseMap).translation();
			/*new JsonMapToHbaseMap(newPrefix, map,
					StringUtils.isBlank(parentHierarchyPrefix) ? childMap
							: hbaseMap).translation();*/
		}
		
		//hbaseMap.put(getKey(key), childMap);
		
		/*if (StringUtils.isBlank(parentHierarchyPrefix)) {
			hbaseMap.put(key, childMap);
		}*/
	}
	
	@SuppressWarnings("unchecked")
	private void mapOperation(String key, Object obj) {
		Map<String, Object> map = (Map<String, Object>) obj;
		//Map<String, Object> childMap = new HashMap<String, Object>();
		String newPrefix = parentHierarchyPrefix + "_0";
		
		logger.debug("map对象为：{}", map);
		
		new JsonMapToHbaseMap(newPrefix, map, hbaseMap).translation();
		/*new JsonMapToHbaseMap(newPrefix, map,
				StringUtils.isBlank(parentHierarchyPrefix) ? childMap
						: hbaseMap).translation();*/
		
		//hbaseMap.put(getKey(key), childMap);
		/*if (StringUtils.isBlank(parentHierarchyPrefix)) {
			hbaseMap.put(key, childMap);
		}*/
	}
	
	private String getKey(String key) {
		if (StringUtils.isBlank(parentHierarchyPrefix)) {
			return key;
		}
		
		return key + parentHierarchyPrefix; 
	}
}
